package com.szp.spCloud.codeService.conroller;

import com.szp.spCloud.codeService.feign.EmailFeignClient;
import com.szp.spCloud.codeService.service.CodeService;
import com.szp.spCloud.pojo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/api/code")
public class CodeController {
    @Autowired
    CodeService codeService;
    @Autowired
    EmailFeignClient emailFeignClient;

    @RequestMapping("/create/{email}")
    public R generateCode(@PathVariable(value="email") String email) {
        boolean res = codeService.saveCode(email);
        if(res){
            return R.ok("验证码发送成功");
        }else{
            return R.error("验证码发送失败");
        }
    }
    @RequestMapping("/validate/{email}/{code}")
    public R validateCode(@PathVariable(value="email") String email,@PathVariable(value="code") String code) {
        int res = codeService.validateCode(email,code);
        if(res == 0)
            return R.ok("验证成功");
        else if(res == 1)
            return R.error("验证码错误");
        else
            return R.error("验证码超时");
    }

    @RequestMapping("/test")
    public boolean test() {
        return emailFeignClient.test();
    }

    public static void main(String[] args) {
        System.out.println(UUID.randomUUID().toString().replace("-", "").substring(0, 6));
    }
}
